<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  users
 */

class m_users_actions_block_UserEditAction extends m_backoffice_extends_Action
{
	public function execute()
	{

		$tabNameUser = $this->getRequestParameter("tabName");
		$tabbed = $this->getRequestParameter("tabbed");

        $user = $this->getRequestDocument();
        $superUser = $this->getDocumentProvider()->loadDocuments("modules_users/user")->retrieveFirstDocument();
		
        $formProvider = f_form_Provider::getInstance();

        $formProvider->setDocumentName("users")
                     ->setModuleName("users")
                     ->setFormName("FormUsers");

        $values = $formProvider->getParams();
        if($formProvider->isSubmitted())
        {
            if($formProvider->isValid())
            {
                if(isset($values['formValues']['status'])) $user->setPublicationStatus($values['formValues']['status']);

                $user
                    ->setAuthor($this->getUser()->getlogin())
                    ->setFirstname($values['formValues']['prenom'])
                    ->setLastname($values['formValues']['nom'])
                    ->setEmail($values['formValues']['mail']);

                    if(isset($values['formValues']['culture']))
                    {
                        $user->setBackLang($values['formValues']['culture']);

                        if($values['formValues']['culture'] != $this->getDisplayLang())
                        {
                            $this->setDisplayLang($values['formValues']['culture']);
                            $values['errors']['status'] = "refresh";
                        }
                    }

                    $user->save();

                if($this->getUser()->getUniqueId() === $user->getUniqueId())
                {
                    $this->getUserContext()->setUser($user);
                }
            }
            else
            {
                $values['errors'] = $formProvider->getErrors();
                $values['errors']['status'] = "failed";
            }
        }

        $group = f_relation_Manager::getInstance()->getParent($user->getUniqueId(), "user");
        if($group != null)
        {
            $values["group"] = $group;
        }

        $values["user"] = $user;
        $values["currentUser"] = $this->getUser();
        $values["su"] = $superUser;
		$values["tabNameUser"] = $tabNameUser;
		$values["tabbed"] = $tabbed;
		$values["currentLang"] = $this->getDisplayLang();
        $values['displayLangs'] = m_backoffice_services_BackOfficeService::getAvailableBoLangs();
		$this->setModel($values);
		$this->setView('success');
	}	
	
	public function validate()
	{
        $user = $this->getRequestDocument();
        return ($this->getUserContext()->hasRight("users_Edit") || $this->getUser()->getUniqueId() === $user->getUniqueId());
	}
}